package com.gomore.opple.module.addbankcard;

import com.gomore.opple.R;
import com.gomore.opple.data.DataRepository;
import com.gomore.opple.data.remote.bean.WrapperResponseEntity;
import com.gomore.opple.data.remote.retrofit.HttpResponseFunc;
import com.gomore.opple.data.remote.retrofit.RxSubscriber;
import com.gomore.opple.data.remote.retrofit.ServerResponseFunc;
import com.gomore.opple.exception.ApiException;
import com.gomore.opple.rest.applybill.TOBankBindEntity;
import com.gomore.opple.rest.applybill.TOCommAuthEntity;
import com.gomore.opple.web.cgform.consumer.entity.TOConsumerEntity;

import javax.inject.Inject;

import okhttp3.MultipartBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

public class AddBankCardPresenter implements AddBankCardContract.Presenter {

    private DataRepository mDataRepositroy;
    private final AddBankCardContract.View mView;
    private CompositeSubscription mSubscriptions;

    @Inject
    AddBankCardPresenter(DataRepository dataRepository, AddBankCardContract.View view) {
        mDataRepositroy = dataRepository;
        mView = view;
        mSubscriptions = new CompositeSubscription();
        mView.setPresenter(this);
    }


    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        mSubscriptions.clear();
    }

    /**
     * 初始化数据
     */
    @Override
    public void prepareInitData() {

    }

    @Override
    public void commAuth(TOCommAuthEntity toCommAuthEntity) {
        unsubscribe();
        mView.showProgressDialog();
        Subscription subscription = mDataRepositroy.commAuth(toCommAuthEntity)
                .map(new ServerResponseFunc<String>())
                .onErrorResumeNext(new HttpResponseFunc<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<String>() {
                    @Override
                    public void onNext(String string) {
                        super.onNext(string);
                        mView.hideProgressDialog();
                        mView.showSecondPage();
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                        mView.showMessage(ex.getMessage());
                    }
                });
        mSubscriptions.add(subscription);
    }

    @Override
    public void bind(TOBankBindEntity toBankBindEntity) {
        unsubscribe();
        mView.showProgressDialog();
        Subscription subscription = mDataRepositroy.bind(toBankBindEntity)
                .map(new ServerResponseFunc<String>())
                .onErrorResumeNext(new HttpResponseFunc<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<String>() {
                    @Override
                    public void onNext(String string) {
                        super.onNext(string);
                        mView.hideProgressDialog();
                        mView.showMessage("绑定银行卡成功");
                        mView.finishActivity();
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                        mView.showMessage(ex.getMessage());
                    }
                });
        mSubscriptions.add(subscription);
    }

    @Override
    public void sendCode(String mobile) {
        unsubscribe();
        mView.showProgressDialog();
        String username = mDataRepositroy.getUser().getEmployee().getUserName();
        Subscription subscription = mDataRepositroy.bankCardBindSendSms(mobile, username)
                .map(new ServerResponseFunc<String>())
                .onErrorResumeNext(new HttpResponseFunc<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<String>() {
                    @Override
                    public void onNext(String version) {
                        super.onNext(version);
                        mView.hideProgressDialog();
                        mView.showMessage(R.string.send_code_success);
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                        mView.showMessage(R.string.send_code_success);
                    }
                });
        mSubscriptions.add(subscription);
    }
}
